<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增角色</title>
    <link href="${basePath }/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link href="${basePath }/static/plugins/ztree/css/metroStyle/metroStyle.css" rel="stylesheet">
    <style>
        #auth-load{
            font-size: 23px;
            position: relative;
            top: 4px;
            left: 4px;
        }
    </style>
</head>
<body>
<form id="user-form" class="layui-form" action="javascript:;" style="margin: 24px 48px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <div class="parentCls">
                <input type="text" name="roleName" placeholder="请输入名称" class="layui-input inputElem">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-block">
            <i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" id="auth-load">&#xe63e;</i>
            <ul id="authtree" class="ztree"></ul>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="form">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${basePath }/static/plugins/layui/layui.js"></script>
<script src="${basePath }/static/plugins/jq/jquery-2.2.4.min.js"></script>
<script src="${basePath }/static/plugins/ztree/js/jquery.ztree.all.js"></script>
<script src="${basePath }/static/build/js/common.js"></script>
<script>
    layui.use(['form', 'layer'], function() {
        var form = layui.form,
            layer = layui.layer;

        //加载ztree
        $.ajax({
            url: '${basePath}/power/menu/getMenuTree',
            data: {

            },
            type: 'post',
            dataType: 'json',
            success: function (res) {
                $('#auth-load').remove();
                var setting = {
                    check: {
                        enable: true
                    }
                };
                $.fn.zTree.init($("#authtree"), setting, res);
            }
        });

        form.render();
        form.on('submit(form)', function(data){
            var reqData = data.field;

            //获取选中菜单id
            var treeObj = $.fn.zTree.getZTreeObj("authtree");
            var nodes = treeObj.getCheckedNodes(true);
            var ids = "";
            for(var i = 0; i < nodes.length; i++){
                ids += (nodes[i].id + ",");
            }
            reqData.menuIdsStr = ids;

            $.ajax({
                url: '${basePath}/power/role/insert',
                type: 'post',
                dataType: 'json',
                data: reqData,
                success: function(resp){
                    if(resp.code==10000){
                        layer.msg('新增成功',{
                            time: 1000
                        }, function(){
                            $('#orm button[type="reset"]').trigger('click');
                        })
                    }
                }
            })

            return false;
        });
    });
</script>
</body>
</html>
